﻿using System.Collections.Generic;
using System.Windows;
using System.Xml;
using Wix.Win8SetupBootstrapper.Classes.Configuration;
using Wix.Win8SetupBootstrapper.Enums;
using Wix.Win8SetupBootstrapper.ViewModels;
using Wix.Win8SetupBootstrapper.Views;
using Microsoft.Tools.WindowsInstallerXml.Bootstrapper;

namespace Wix.Win8SetupBootstrapper.Classes
{
    public abstract class PageController
    {
        private readonly RootViewModel _rootViewModel;

        private LinkedList<Pages> _pageSequence;
        private LinkedListNode<Pages> _currentPage;

        public Pages NextPage
        {
            get
            {
                if (this._currentPage != null && this._currentPage.Next != null)
                {
                    return this._currentPage.Next.Value;
                }

                return Pages.None;
            }
        }

        public Pages PreviousPage
        {
            get
            {
                if (this._currentPage != null && this._currentPage.Previous != null)
                {
                    return this._currentPage.Previous.Value;
                }
                return Pages.None;
            }
        }

        protected abstract string GetSequenceBaseNodeName();

        protected PageController(RootViewModel rootViewModel)
        {
            this._rootViewModel = rootViewModel;

            this.InitializePageSequence();
        }

        private void InitializePageSequence()
        {
            this._pageSequence = new LinkedList<Pages>();
            XmlNode configurationNode = ConfigurationLoader.GetConfigurationNode(this.GetSequenceBaseNodeName());
            foreach (XmlNode xmlNode in configurationNode.ChildNodes)
            {
                if (string.Equals(xmlNode.Name, "WelcomePage"))
                {
                    this._pageSequence.AddLast(Pages.WelcomePage);
                }
                else
                {
                    if (string.Equals(xmlNode.Name, "FeaturesPage"))
                    {
                        this._pageSequence.AddLast(Pages.FeaturesPage);
                    }
                    else
                    {
                        if (string.Equals(xmlNode.Name, "ConfigurationPage"))
                        {
                            this._pageSequence.AddLast(Pages.ConfigurationPage);
                        }
                        else
                        {
                            if (string.Equals(xmlNode.Name, "ProgressPage"))
                            {
                                this._pageSequence.AddLast(Pages.ProgressPage);
                            }
                            else
                            {
                                if (string.Equals(xmlNode.Name, "FinishPage"))
                                {
                                    this._pageSequence.AddLast(Pages.FinishPage);
                                }
                                else
                                {
                                    if (string.Equals(xmlNode.Name, "FinishErrorPage"))
                                    {
                                        this._pageSequence.AddLast(Pages.FinishErrorPage);
                                    }
                                    /* else
                                                {
                                                    if (string.Equals(xmlNode.Name, "FinishErrorPage"))
                                                    {
                                                        this._pageSequence.AddLast(Pages.FinishPage);
                                                    }
                                                }*/
                                }
                            }
                        }
                    }
                }
            }
        }

        public void SetFirstPage()
        {
            this.SetPage(this._pageSequence.First.Value);
        }

        public void SetPage(Pages newPage)
        {
            var newCurrentPage = this._pageSequence.Find(newPage);

            if (newCurrentPage == this._currentPage)
                return;

            this._currentPage = newCurrentPage;

            FrameworkElement element = null;

            if (newPage == Pages.WelcomePage)
            {
                this._rootViewModel.Bootstrapper.Engine.Log(LogLevel.Standard, "Initializing WelcomePage");
                element = new WelcomePageView(_rootViewModel);
            }
            if (newPage == Pages.FeaturesPage)
            {
                this._rootViewModel.Bootstrapper.Engine.Log(LogLevel.Standard, "Initializing Features Page");
                element = new FeaturesPageView(_rootViewModel);
            }
            if (newPage == Pages.ConfigurationPage)
            {
                this._rootViewModel.Bootstrapper.Engine.Log(LogLevel.Standard, "Initializing Configuration Page");

                element = new ConfigurationPageView(_rootViewModel);

            }
            if (newPage == Pages.ProgressPage)
            {
                this._rootViewModel.Bootstrapper.Engine.Log(LogLevel.Standard, "Initializing Progress Page");

                element = new ProgressPageView(_rootViewModel);
            }

            if (newPage == Pages.FinishPage)
            {
                this._rootViewModel.Bootstrapper.Engine.Log(LogLevel.Standard, "Initializing Finish Page");

                element = new FinishPageView(_rootViewModel);
            }

            if (newPage == Pages.FinishErrorPage)
            {
                this._rootViewModel.Bootstrapper.Engine.Log(LogLevel.Standard, "Initializing Finish Page");

                element = new FinishErrorPageView(this._rootViewModel);
            }


            this._rootViewModel.CurrentControl = element;
        }
    }
}